package com.seeyon.apps.common.utils;

import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.DateUtil;
import com.seeyon.ctp.util.FlipInfo;
import com.seeyon.ctp.util.JDBCAgent;
import org.apache.commons.logging.Log;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JDBCAgentUtils
 * @Description JDBCAgent封装工具类
 * @Author CheungHowrAn
 * @Date 2024/3/20 17:18
 * @PackageName com.seeyon.apps.common.utils
 * @ProjectName 常用工具类
 */
public class JDBCAgentUtils {

    private static final Log log = CtpLogFactory.getLog(JDBCAgentUtils.class);

    /**
     * @MethodName: query
     * @Description: 查询
     * @CreateDate: 2025/6/27 10:42
     * @Author: CheungHowrAn
     * @Param sql
     * @Param params
     * @Param flipInfo
     * @Return FlipInfo
     */
    public static FlipInfo query(String sql, List params, FlipInfo flipInfo) {
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            flipInfo = jdbcAgent.findByPaging(sql, params, flipInfo);
        } catch (Exception e) {
            log.error("查询异常", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return flipInfo;
    }


    /**
     * @MethodName: query
     * @Description: 查询
     * @CreateDate: 2025/6/27 10:42
     * @Author: CheungHowrAn
     * @Param sql
     * @Param flipInfo
     * @Return FlipInfo
     */
    public static FlipInfo query(String sql, FlipInfo flipInfo) {
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            flipInfo = jdbcAgent.findByPaging(sql, flipInfo);
        } catch (Exception e) {
            log.error("查询异常", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return flipInfo;
    }

    /**
     * @MethodName query
     * @Description 查询
     * @param: sql
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @Author CheungHowrAn
     * @Date 2024/3/20 17:33
     */
    public static List<Map<String, Object>> query(String sql) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            jdbcAgent.execute(sql);
            resultList = jdbcAgent.resultSetToList();
        } catch (Exception e) {
            log.error("查询异常", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return resultList;
    }

    /**
     * @MethodName query
     * @Description 查询
     * @param: sql
     * @param: params
     * @return: java.util.List<java.util.Map < java.lang.String, java.lang.Object>>
     * @Author CheungHowrAn
     * @Date 2024/3/20 17:33
     */
    public static List<Map<String, Object>> query(String sql, List params) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            jdbcAgent.execute(sql, params);
            resultList = jdbcAgent.resultSetToList();
        } catch (Exception e) {
            log.error("查询异常", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return resultList;
    }

    /**
     * @MethodName execute
     * @Description 执行SQL
     * @param: sql
     * @return: java.lang.Integer
     * @Author CheungHowrAn
     * @Date 2024/3/20 17:33
     */
    public static Boolean execute(String sql) {
        Boolean execute = true;
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            jdbcAgent.execute(sql);
        } catch (Exception e) {
            execute = false;
            log.error("执行SQL异常：", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return execute;
    }

    /**
     * @MethodName execute
     * @Description 执行SQL
     * @param: sql
     * @param: params
     * @return: java.lang.Integer
     * @Author CheungHowrAn
     * @Date 2024/3/20 17:33
     */
    public static Boolean execute(String sql, List params) {
        Boolean execute = true;
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            jdbcAgent.execute(sql, params);
        } catch (Exception e) {
            execute = false;
            log.error("执行SQL异常：", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return execute;
    }

    /**
     * @MethodName executeBatch
     * @Description 批量执行
     * @param: sqlList
     * @return: java.lang.Integer
     * @Author CheungHowrAn
     * @Date 2024/3/20 17:34
     */
    public static Boolean executeBatch(List<String> sqlList) {
        Boolean execute = true;
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);
            jdbcAgent.executeBatch(sqlList);
        } catch (Exception e) {
            execute = false;
            log.error("批量执行（占位符）异常", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return execute;
    }

    /**
     * @MethodName executeBatch
     * @Description
     * @param: sql 带占位符（?）的SQL
     * @param: params
     * @return: java.lang.Integer
     * @Author CheungHowrAn
     * @Date 2024/8/9 10:55
     */
    public static Boolean executeBatch(String sql, List<List> params) {
        Boolean execute = true;
        JDBCAgent jdbcAgent = null;
        try {
            jdbcAgent = new JDBCAgent(false, false);

            jdbcAgent.batch1Prepare(sql);
            for (List param : params) {
                jdbcAgent.batch2Add(param);
            }
            jdbcAgent.batch3Execute();
        } catch (Exception e) {
            execute = false;
            log.error("批量执行异常：", e);
        } finally {
            if (jdbcAgent != null) {
                jdbcAgent.close();
            }
        }
        return execute;
    }

    public static String getTimeQueryCriteria(String columnName, Date startDate, Date endDate) {
        StringBuilder sql = new StringBuilder(" ").append(columnName).append(" BETWEEN ");
        if (JDBCAgent.isOracleRuntime()) {
            sql.append("TO_DATE('").append(DateUtil.format(startDate, "yyyy-MM-dd HH:mm:ss")).append("', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('").append(DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss")).append("', 'YYYY-MM-DD HH24:MI:SS')");
        } else if (JDBCAgent.isMySQLRuntime()) {
            sql.append("'").append(DateUtil.format(startDate, "yyyy-MM-dd HH:mm:ss")).append("' AND '").append(DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss")).append("'");
        } else if (JDBCAgent.isSQLServerRuntime()) {
            sql.append("'").append(DateUtil.format(startDate, "yyyy-MM-dd'T'HH:mm:ss")).append("' AND '").append(DateUtil.format(endDate, "yyyy-MM-dd'T'HH:mm:ss")).append("'");
        } else if (JDBCAgent.isPostgreSQLRuntime()) {
            sql.append("'").append(DateUtil.format(startDate, "yyyy-MM-dd HH:mm:ss")).append("'::timestamp AND '").append(DateUtil.format(endDate, "yyyy-MM-dd HH:mm:ss")).append("'::timestamp");
        }
        return sql.toString();
    }
}
